//
//  OilHistoryViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/26.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
import CRRefresh

class OilHistoryViewController: BaseViewController {

    @IBOutlet var addOilNumLabel: UILabel!
    
    @IBOutlet var recomeLabel: UILabel!
    
    @IBOutlet var oilHistoryTableview: UITableView!
    
    let oilHistoryCell = "oilHistoryCell"
    var oilHistorys :[OilModel]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        page = 1
        requstOilHistoryList(page: page!)
        configView()
    }
    
    func configView() -> Void {
        oilHistoryTableview.delegate = self
        oilHistoryTableview.dataSource = self
        oilHistoryTableview.separatorStyle = .none
        oilHistoryTableview.showsVerticalScrollIndicator = false
        oilHistoryTableview.backgroundColor = BaseColor.BackGroundColor
        oilHistoryTableview.register(UINib(nibName: "OilHistoryTableViewCell", bundle: nil), forCellReuseIdentifier: oilHistoryCell)
        oilHistoryTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
            page = 1
            self?.requstOilHistoryList(page: page!)
        }
        oilHistoryTableview.cr.addFootRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol)  { [weak self] in
            page! += 1
            self?.requstOilHistoryList(page: page!)
        }
    }
    
    //网络请求
    //请求加油历史记录
    func requstOilHistoryList(page:Int) -> Void {
        
        Alamofire.request(getGasstationListUrl, method:.post, parameters: ["page": page,"sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
                
                self.oilHistoryTableview?.cr.endHeaderRefresh()
                self.oilHistoryTableview?.cr.endLoadingMore()
                switch response.result.isSuccess {
                case true:

                    var basemodel = BaseResponseOfModel<BaseArrayData<OilModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!

                    switch basemodel.status! {
                    case true:
                        self.addOilNumLabel.text =  (basemodel.data?.total)!
                        self.recomeLabel.text = (basemodel.data?.total_price)!
                        if page == 1 {
                            self.oilHistorys?.removeAll()
                            self.oilHistorys = basemodel.data?.data
                        }else {
                            if ((basemodel.data?.data?.count)! > 0) {
                                self.oilHistorys! += (basemodel.data?.data)!
                            }else {
                                return
                            }
                        }
                        self.oilHistoryTableview.reloadData()
                        print("数据成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
                
                
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

extension OilHistoryViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.oilHistorys?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: oilHistoryCell, for: indexPath) as! OilHistoryTableViewCell
        cell.selectionStyle = .none
        let model = self.oilHistorys![indexPath.item]
        cell.oilGunNumLabel.text = "油枪号：" + model.product + "号"
        cell.oilTimeLabel.text = "加油时间：" + model.create_at
        cell.oilPriceLabel.text = "加油金额：" + model.price + "元"
        
        return cell
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(70)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    //    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //        return 10
    //    }
    
}
